<?php
App::uses('AppController', 'Controller');

class SeriesController extends AppController{
    public $components=array('Paginator');
    public $uses=array('Serie');
    public $layout='sib';
    
    public function index(){
        $this->Serie->recursive=0;
        $this->set('series', $this->Paginator->paginate());
    }
    
    public function view ($id=null){
        if(!$this->Series->exists($id)){
            throw new NotFoundException (_('Serie Invalida')); 
        }
        $options=array('conditions' => array('Series' . $this->Series->primaryKey=>$id));
        $this->set('serie' , $this->Series->find('first',$options));        
        }
        
    function serie(){
        if(!empty($this->request->data))  
        {
            //debug($this->request->data); die;
            $this->request->data['Serie']['numero'] = $this->request->data['Serie']['desde'];
        $this->Serie->create();
        if($this->Serie->save($this->request->data['Serie']))
            {
            $this->Session->setFlash('Registro Realizado con exito',mensajeBueno);
            $this->redirect(array('action'=>'index'),null ,true);
            }else{
                
                $this->Session->setFlash('Registro Denegado',mensajeError);
            }
        }
    }
    
    public function editar($id=null){
        $this->Serie->id=$id;
        if(!$this->Serie->exists()){
            throw new NotFoundException(_('No Existe'));
        }
        if($this->request->is(array('post','put'))){
            //debug($this->request->data); die;
            if($this->Serie->save($this->request->data)){
                $this->Session->setFlash('Modificacion Exitosa',mensajeBueno);
                $this->redirect(array('action'=>'index'),null ,true);
            }else {
                $this->Session->setFlash('Error al Guardar',mensajeError);
            }
        }  else {
            //$this->request->data =  $this->Serie->read(null, $id);
            $this->Serie->id=$id;
            $this->request->data  =  $this->Serie->read();
        }
    }
    
   public function delete ($id=null){
        $this->Serie->id=$id;
        $this->request->data =  $this->Serie->read();
        if(!$id){
            $this->Session->setFlash('El Numero de Serie No Existe',mensajeError);
            $this->redirect(array('action'=>'index') ,null ,true);
        }else{
            if($this->Serie->delete($id)){
                
                $this->Session->setFlash('Se Elimino el Numero de Serie',mensajeBueno);
                $this->redirect(array('action'=>'index'));
            }else{
                $this->Session->setFlash('Error al Eliminar',mensajeError);
            }
            
        }
    }
}

